#include "cpp-httplib/httplib.h"
#include "search.hpp"
#include "Daemon.hpp"

const std::string input = "./data/raw_html/raw.txt";
const std::string root_path = "./wwwroot";

int main()
{
    Set_Daemon();
    Search search;
    search.InitSearch(input);
    httplib::Server svr;
    svr.set_base_dir(root_path.c_str());
    //http://43.138.85.14:8081/s?word=split,?表示字符串查询的开始
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &rsp)
            {
                if (!req.has_param("word"))
                {
                    rsp.set_content("必须要有搜索关键字!", "text/plain; charset=utf-8");
                    return;
                }
                std::string word = req.get_param_value("word");
                std::cout << "用户在搜索：" << word << std::endl;
                std::string json_string = search.get_param_value(word); //, &json_string);
                rsp.set_content(json_string, "application/json");
                // rsp.set_content("你好,世界!", "text/plain; charset=utf-8");
            });
    svr.listen("0.0.0.0", 8081);
    return 0;
}